<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Member extends MY_Controller {

	/**
	 * Member controller 
	 */
    public function __construct(){
        parent::__construct();
        $this->load->model('member_model');
        $this->load->library('flexigrid');
        $this->load->helper('flexigrid');
    }
	public function index()
	{
        //echo "Welcome to Sobic Administrator";
        $this->view('list');
	}
    
    function list_member(){
        
        //ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		
        $colModel['memberId'] = array('ID',50,TRUE,'center',2);
		$colModel['username'] = array('Tên đăng nhập',150,TRUE,'center',0);
		$colModel['firstName'] = array('Họ đệm ',150,TRUE,'left',1);
		$colModel['lastName'] = array(' Tên  ',100,TRUE,'left',0);
		$colModel['departmentId'] = array('Phòng ban',150, TRUE,'left',0);
		$colModel['email'] = array(' Email ',200, TRUE, 'left',1);
        
		
		
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 900,
		'height' => 400,
		'rp' => 15,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Hello',
		'usepager'=> true,
		'useRp' => true,
		'showTableToggleBtn' => true,
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		$buttons[] = array('Delete','delete','test');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','test');
		$buttons[] = array('DeSelect All','delete','test');
		$buttons[] = array('separator');

		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/member/ajax"),$colModel,'id','asc',$gridParams,$buttons); 
		
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$this->view('flexi_views',$data);	
        
    }
    
    function register(){
		//debug($this);
		$this->loadModuleJs('memberController.js');
		$this->template->title("Đăng ký tài khoản");
		$this->loginView('register_form2');  
	}
    
    function exe_register(){
       
        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        
        //debug($_POST['firstName']);
        $message = "";
        $check = 1;
        $data1 = $this->member_model->getMember($username);
        if(!empty($data1))
        {
            $message .= "Username đã tồn tại <br/> ";
            $check = 0;
        }
        $data2=$this->member_model->getMemberByEmail($email);
        if(!empty($data2))
        {
            $check = 0;
            $message .= "Email đã tồn tại <br/> ";
        }
        
        if($check==1){
            $objMember=array(
            'firstName'=>$firstName,
            'lastName'=>$lastName,
            'username'=>$username,
            'password'=>md5($password.SALT),
            'email'=>$email,
            'departmentId'=>1
            );  
           // debug($objMember);
            $this->member_model->insertMember($objMember);  
        }
        $return = array(
            'check'=>$check,
            'message'=>$message,
            );   
        echo json_encode($return);
    }
    
    function check_user_name(){
        $username = $_POST['username'];
        $data2 = $this->member_model->getMember($username);
        if(!is_object($data2))
        {
            echo json_encode(true);
        }else
        {
            echo json_encode(false);    
        }
    }
    function check_email(){
        $email = $_POST['email'];
        $data2 = $this->member_model->getMemberByEmail($email);
        if(!is_object($data2))
        {
            echo json_encode(true);
        }else
        {
            echo json_encode(false);    
        }
        
    }
    
    function add_member(){
        $html=$this->baseView('add_form','',true);
        echo json_encode(array('html'=>$html));  
    }
    
    function save(){
        $arrayToJs = array();
	   $arrayToJs[0] = "form";
       $arrayToJs[1]=true;
        echo  json_encode($arrayToJs);
    }
    
 
}


?>